 package cn.edu.tju.se.records.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.edu.tju.se.records.service.PhotoService;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import cn.edu.tju.se.auth.domain.User;
import cn.edu.tju.se.base.service.DateAdapter;
import cn.edu.tju.se.base.web.controller.BaseController;
import cn.edu.tju.se.records.domain.Patient;
import cn.edu.tju.se.records.service.PatientService;

@Controller
@RequestMapping(value="/patient")
public class PatientController extends BaseController{
	@Autowired
	PatientService patientService;

	@Autowired
	PhotoService photoService;

	public void setPhotoService(PhotoService photoService) { this.photoService = photoService; }

	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	@RequestMapping(method=RequestMethod.POST,value="/patientid")
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody Map addPatient(@RequestBody String body,HttpServletRequest request) {
		Map map = new HashMap<String ,String >();
		String msg = "";

		Gson gson = null;
		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new DateAdapter());
		builder.setDateFormat("yyyy-MM-dd");
		gson = builder.create();
		
		Patient patient = gson.fromJson(body, Patient.class);
		if(patientService.findPatientByPatientId(patient.getPatientid())!=null){
			msg="该用户病历号已存在";
		}else{
			User user = getSessionUser(request);
			patient.setDoctorid(user.getUserid());
			patient.setTreatcondition("进入治疗");
			patientService.add(patient);
			msg="添加成功";
		}
		map.put("msg",msg);
		return map;
	}

	@RequestMapping(method=RequestMethod.POST,value="/patientid/{id}")
	@ResponseStatus(HttpStatus.OK)
	public void updatePatient(@RequestBody String body) {

		Gson gson = null;
		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new DateAdapter());
		builder.setDateFormat("yyyy-MM-dd");
		gson = builder.create();

		Patient patient = gson.fromJson(body, Patient.class);
		patientService.update(patient);
	}
	
	@RequestMapping(method=RequestMethod.PUT, value="/{id}")
	@ResponseStatus(HttpStatus.OK)
	public void updatePatientByPut(@RequestBody String body) {
		Gson gson = null;
		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new DateAdapter());
		builder.setDateFormat("yyyy-MM-dd");
		gson = builder.create();
		
		Patient patient = gson.fromJson(body, Patient.class);
		patientService.update(patient);
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/{id}")
	@ResponseStatus(HttpStatus.OK)
	public void updatePatientByPost(@RequestBody String body) {
		Gson gson = null;
		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new DateAdapter());
		builder.setDateFormat("yyyy-MM-dd");
		gson = builder.create();
		
		Patient patient = gson.fromJson(body, Patient.class);
		patientService.update(patient);
	}
	
	@RequestMapping(method=RequestMethod.DELETE, value="/patientid/{id}")
	public @ResponseBody List<Patient> removePatient(@PathVariable String id) {
		photoService.removePhotoFilesbyPatientId(Integer.parseInt(id));
		photoService.removePhotosbyPatientId(Integer.parseInt(id));
		patientService.remove(Integer.parseInt(id));
		List<Patient> patients = patientService.getAll();
		return patients;
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/patientid/{id}")	
	public @ResponseBody Patient getPatientById(@PathVariable String id,Model model) {
		return patientService.findPatientByPatientId(id);
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/patientname/{id}")	
	public @ResponseBody Patient getPatientByName(@PathVariable String id,Model model) {
		return patientService.findPatientByPatientName(id);
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/patientlist/{id}")	
	public @ResponseBody List<Patient> getPatientByDoctor(@PathVariable String id,Model model) {
		return patientService.findPatientByDoctorId(id);
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/patientid")
	public @ResponseBody List<Patient> getPatients(Model model,HttpServletRequest request) {
		User user = getSessionUser(request);
		return patientService.findPatientByDoctorId(user.getUserid());
	}	
}
